<script setup lang="ts">
import { ref, onMounted } from "vue";
import { formRules } from "./utils/rule";
import { FormProps } from "./utils/types";
import type { FormInstance } from "element-plus";
import { tags_list } from "@/api/tag";

const props = withDefaults(defineProps<FormProps>(), {
  formInline: () => ({
    id: "",
    table_at: "",
    version: "",
    tags_id: ""
  })
});

const ruleFormRef = ref<FormInstance>();
const newFormInline = ref(props.formInline);

const labelList = ref([]);

onMounted(() => {
  getLabelList();
});

// 获取标签列表
function getLabelList() {
  tags_list({
    page: 1,
    pagesize: 999,
    status: 1
  }).then(res => {
    labelList.value = res.data;
  });
}

function getRef() {
  return ruleFormRef.value;
}

defineExpose({ getRef });
</script>

<template>
  <div>
    <el-form
      ref="ruleFormRef"
      :rules="formRules"
      :model="newFormInline"
      label-position="top"
    >
      <el-form-item label="" prop="tags_id">
        <el-select
          v-model="newFormInline.tags_id"
          placeholder="请选择"
          clearable
          class="w-full!"
        >
          <el-option
            v-for="(item, index) in labelList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>
